package VIEWS;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import CONTROLLERS.UsersControllers;
import ENTITIESMODELS.UserEntitys;

public class frmLogin extends frmRightForms {

    private JPanel contentPane;
    private JTextField txtTenTruyCap;
    private JPasswordField txtMatKhau;   
    static frmLogin frmlogins;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    frmlogins = new frmLogin(m_objConnection, m_objSecurityContext);
                    frmlogins.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public frmLogin(Connection objConnection, UserEntitys objSecurity) {
        super(objConnection, objSecurity);     

        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage(frmLogin.class.getResource("/IMAGES/login Manager_2.png")));
        setTitle("---- \u0110\u0102NG NH\u1EACP H\u1EC6 TH\u1ED0NG ----");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 524, 194);
        contentPane = new JPanel();
        contentPane.setBorder(new TitledBorder(null, "Th\u00F4ng tin \u0111\u0103ng nh\u1EADp", TitledBorder.LEADING,
                TitledBorder.TOP, null, Color.RED));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("Vui l\u00F2ng nh\u1EADp th\u00F4ng tin \u0111\u0103ng nh\u1EADp:");
        lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblNewLabel.setBounds(21, 25, 264, 22);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setIcon(new ImageIcon(frmLogin.class.getResource("/IMAGES/login_icon_4.png")));
        lblNewLabel_1.setBounds(21, 58, 119, 101);
        contentPane.add(lblNewLabel_1);

        JLabel lblTnTruyCp = new JLabel("T\u00EAn truy c\u1EADp:");
        lblTnTruyCp.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblTnTruyCp.setBounds(117, 58, 89, 22);
        contentPane.add(lblTnTruyCp);

        JLabel lblMtKhu = new JLabel("M\u1EADt kh\u1EA9u:");
        lblMtKhu.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblMtKhu.setBounds(117, 103, 69, 22);
        contentPane.add(lblMtKhu);

        txtTenTruyCap = new JTextField();
        txtTenTruyCap.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent arg0) {

            }
        });
        txtTenTruyCap.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtTenTruyCap.setBounds(201, 60, 293, 20);
        contentPane.add(txtTenTruyCap);
        txtTenTruyCap.setColumns(10);

        txtMatKhau = new JPasswordField();
        txtMatKhau.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent arg0) {
                if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
                    ExecLogin();
                }
            }
        });
        txtMatKhau.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtMatKhau.setBounds(201, 105, 293, 20);
        contentPane.add(txtMatKhau);

        JButton btnThoat = new JButton("Tho\u00E1t");
        btnThoat.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                int Option = JOptionPane.showConfirmDialog(getParent(), "Bạn có chắc chắn thoát không?", "Thông báo",
                        JOptionPane.YES_NO_OPTION);
                if (Option == 0) {
                    dispose();
                }
            }
        });
        btnThoat.setIcon(new ImageIcon(frmLogin.class.getResource("/IMAGES/deleteIcon_2.png")));
        btnThoat.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnThoat.setBounds(395, 136, 99, 23);
        contentPane.add(btnThoat);

        JButton btnDangNhap = new JButton("\u0110\u0103ng Nh\u1EADp");
        btnDangNhap.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent arg0) {
                // Nếu ngư�?i dùng bấm Enter thì sẽ tiến hành đăng nhập
                if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
                    ExecLogin();
                }
            }
        });
        btnDangNhap.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                ExecLogin();
            }
        });
        btnDangNhap.setIcon(new ImageIcon(frmLogin.class.getResource("/IMAGES/action_settings_3.png")));
        btnDangNhap.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnDangNhap.setBounds(258, 136, 127, 23);
        contentPane.add(btnDangNhap);
    }

    // Thực thi truy vấn Login
    void ExecLogin() {
        if (txtTenTruyCap.getText().isEmpty()) {
            txtTenTruyCap.setFocusable(true);
            JOptionPane.showMessageDialog(getParent(), "Tên truy cập không được để trống!", "Thông báo",
                    JOptionPane.WARNING_MESSAGE);
        } else if (txtMatKhau.getText().isEmpty()) {
            txtMatKhau.setFocusable(true);
            JOptionPane.showMessageDialog(getParent(), "Mật khẩu không được để trống!", "Thông báo",
                    JOptionPane.WARNING_MESSAGE);
        } else {
            m_objUserController = new UsersControllers(m_objConnection, m_objSecurityContext);
            try {
                UserEntitys obj = new UserEntitys();
                obj.setUserName(txtTenTruyCap.getText());
                obj.setPasswords(txtMatKhau.getText());
                m_objUserController.ExecOfCheckElement(obj);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo", JOptionPane.WARNING_MESSAGE);
            }
            if (m_objSecurityContext.isLoginSuccess()) {
                frmMainApp frm = new frmMainApp();
                frm.SetLoginInfo(m_objSecurityContext);
                frm.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frm.setVisible(true);

                frmlogins = new frmLogin(m_objConnection, m_objSecurityContext);
                frmlogins.setVisible(false);

                // Giải phóng form login
                dispose();
                // System.exit(HIDE_ON_CLOSE);
            } else {
                JOptionPane.showMessageDialog(getParent(), "Thông tin tài khoản không đúng.Vui lòng nhập lại!",
                        "Thông báo", JOptionPane.WARNING_MESSAGE);
            }
        }
    }
}
